package de.openkanban.server.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import de.openkanban.server.domain.Activity;
import de.openkanban.server.interceptor.annotation.SecurityInterception;
import de.openkanban.server.service.ActivityService;
import de.openkanban.server.service.KanbanService;
import de.openkanban.server.session.OpenKanbanSessionAttributes;
import de.openkanban.shared.dto.ActivityDto;
import de.openkanban.shared.dto.KanbanDto;
import de.openkanban.shared.dto.StatusDto;
import de.openkanban.shared.util.StatusCodes;

@Controller
@RequestMapping(value = "/activity")
public class ActivityController {

	@Autowired
	private ActivityService activityService;

	@Autowired
	private KanbanService kanbanService;

	@Autowired
	private OpenKanbanSessionAttributes sessionAttributes;

	@Autowired
	private DozerBeanMapper mapper;

	@RequestMapping(method = RequestMethod.POST, value = "/{parentActivityId}/kanban/all")
	@ResponseBody
	@SecurityInterception
	public List<KanbanDto> getAllKanbans(@PathVariable Long parentActivityId, HttpServletRequest req, HttpServletResponse resp) {
		List<KanbanDto> kanbanDtoList = kanbanService.getAllByActivityId(parentActivityId);
		return kanbanDtoList;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/update")
	@ResponseBody
	@SecurityInterception
	public ActivityDto updateActivity(ActivityDto activityDto, HttpServletRequest req, HttpServletResponse resp) {
		Activity activity = mapper.map(activityDto, Activity.class);
		activity = activityService.updateActivity(activity);
		return mapper.map(activity, ActivityDto.class);
	}

	@RequestMapping(method = RequestMethod.POST, value = "{activityId}/remove")
	@ResponseBody
	@SecurityInterception
	public StatusDto removeActivity(@PathVariable Long activityId, HttpServletRequest req, HttpServletResponse resp) {
		StatusCodes status = activityService.removeActivity(activityId);
		return new StatusDto(status.getStatusCode(), status.getStatusText());
	}

	@RequestMapping(method = RequestMethod.POST, value = "{parentActivityId}/kanban/add")
	@ResponseBody
	@SecurityInterception
	public KanbanDto addKanbanToActivity(@PathVariable Long parentActivityId, KanbanDto kanbanDto, HttpServletRequest req,
			HttpServletResponse resp) {
		return activityService.addKanbanToActivity(sessionAttributes.getUserId(req), parentActivityId, kanbanDto);
	}

	@RequestMapping(method = RequestMethod.POST, value = "{newParentActivityId}/kanban/{kanbanId}/move")
	@ResponseBody
	@SecurityInterception
	public KanbanDto moveKanbanToActivity(@PathVariable Long newParentActivityId, @PathVariable Long kanbanId, HttpServletRequest req,
			HttpServletResponse resp) {
		KanbanDto kanbanDto = activityService.moveKanbanToActivity(sessionAttributes.getUserId(req), newParentActivityId, kanbanId);
		return kanbanDto;
	}

}
